package edu.uwg.lewiswilliams.android.assignment2;

/**
 * Calculator handles conversion and other mathematical operations for the UI'S
 * 
 * @author Lewis Williams
 * @version
 */
public class Calculator {
	
	/**
	 * Converts in any direction from hex to decimal to binary. 
	 * 
	 * @param convertingTo
	 * 			what the value is being converted to ("hex", "dec", or "bin")
	 * @param convertingFrom
	 * 			what the value is being converted from ("hex", "dec", or "bin")
	 * @param value
	 * 			the value to be converted
	 * @return
	 * 			the converted value in string format for display only
	 */
	public String convert(String convertingTo, String convertingFrom, String value){
		String finalValue = "";
		
		if(convertingTo.equalsIgnoreCase(convertingFrom)){
			finalValue = value;
		}
		else if(convertingTo.equalsIgnoreCase("Hex") && convertingFrom.equalsIgnoreCase("Dec")){
			int convertingValue = Integer.valueOf(value);
			finalValue = Integer.toHexString(convertingValue);
		}
		else if(convertingTo.equalsIgnoreCase("Bin") && convertingFrom.equalsIgnoreCase("Dec")){
			int convertingValue = Integer.valueOf(value);
			finalValue = Integer.toBinaryString(convertingValue);
		}
		else if(convertingTo.equalsIgnoreCase("Dec") && convertingFrom.equalsIgnoreCase("Hex")){
			int convertingValue = Integer.parseInt(value, 16);
			finalValue = Integer.toString(convertingValue);
		}
		else if(convertingTo.equalsIgnoreCase("Bin") && convertingFrom.equalsIgnoreCase("Hex")){
			int convertingValue = Integer.parseInt(value, 16);
			finalValue = Integer.toBinaryString(convertingValue);
		}
		else if(convertingTo.equalsIgnoreCase("Hex") && convertingFrom.equalsIgnoreCase("Bin")){
			int convertingValue = Integer.parseInt(value, 2);
			finalValue = Integer.toHexString(convertingValue);
		}
		else if(convertingTo.equalsIgnoreCase("Dec") && convertingFrom.equalsIgnoreCase("Bin")){
			int convertingValue = Integer.parseInt(value, 2);
			finalValue = Integer.toString(convertingValue);
		}
		
		return finalValue;
	}
	
	/**
	 * Adds, subtracts, multiplies, or divides any two integers.
	 * 
	 * @param operator
	 * 			the operation to be performed ("+","-","*", or "/")
	 * @param firstOperand
	 * 			the first operand of the equation
	 * @param secondOperand
	 * 			the second operand of the equation
	 * @return
	 * 			the final value of the operation that was performed
	 */
	public int operation(String operator, int firstOperand, int secondOperand){
		if(operator == null){
			throw new IllegalArgumentException();
		}
		int finalValue = -1;
		
		if(operator.equalsIgnoreCase("+")){
			finalValue = firstOperand + secondOperand;
		}
		else if(operator.equalsIgnoreCase("-")){
			finalValue = firstOperand - secondOperand;
		}
		else if(operator.equalsIgnoreCase("x")){
			finalValue = firstOperand * secondOperand;
		}
		else if(operator.equalsIgnoreCase("/")){
			if(secondOperand == 0){
				throw new IllegalArgumentException();
			}
			finalValue = firstOperand / secondOperand;
		}
		else{
			
		}
		
		return finalValue;
	}

}
